package com.gxa.user.recycling.service.impl;

import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.*;
import com.gxa.core.recycling.service.impl.BaseServiceImpl;
import com.gxa.user.recycling.dao.GoodsDao;
import com.gxa.user.recycling.mapper.GoodsorderMapper;
import com.gxa.user.recycling.repository.GoodsRepository;
import com.gxa.user.recycling.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author xhy
 * @Date 2021/12/2 10:22
 */
@Service("goodsorderService")
@Transactional(rollbackFor = {Throwable.class})
public class GoodsorderServiceImpl extends BaseServiceImpl<Goodsorder> implements GoodsorderService {
    @Resource
    GoodsorderMapper goodsorderMapper;
    @Override
    public List<Map> findGoodsorder(Long id, String state) {
        return goodsorderMapper.findGoodsorder(id, state);
    }
    @Autowired
    GoodsRepository goodsRepository;


    @Override
    public List<Goods> selectByMongodb() {
        List<Goods> all = goodsRepository.findAll();
        return all;
    }

    @Override
    public Goods selectById(Long id) throws BusinessException {
        Goods goods = goodsRepository.findGoodsByIdEquals(id);
        if(goods==null){
            throw new BusinessException(ExceptionCode.NOFIND_GOODS);
        }
        return goods;
    }
    @Autowired
    GoodsService goodsService;
    @Autowired
    UsersService usersService;
    @Autowired
    ScoreService scoreService;
    @Autowired
    CommunityManageService communityManageService;
    @Autowired
    ServiceAddressService serviceAddressService;
    @Autowired
    GoodsorderService goodsorderService;
    @Autowired
    GoodsDao goodsDao;

    @Override
    public Integer exchange(Long id, Integer points,Long gid,String words,String address,Long communityId) throws BusinessException {
        User user = usersService.selectById(id);
        Integer nowPoints = user.getNowPoints();
        Integer now = user.getNowPoints()-points;
        if(now<0){
            throw new BusinessException(ExceptionCode.MONEY_BUZU);
        }
        user.setNowPoints(user.getNowPoints()-points);
        Goods goods = goodsService.selectById(gid);
        String name = goods.getName();
        usersService.updateById(user);
        //生成积分明细
        Score score = new Score();
        score.setUid(id);
        score.setChangetime(new Date(System.currentTimeMillis()));
        score.setNum(points);
        score.setSerson("回收"+name);
        score.setStatus(false);
        scoreService.insert(score);
        Goodsorder goodsorder = new Goodsorder();
        goodsorder.setExchangetime(new Date(System.currentTimeMillis()));
        goodsorder.setGid(gid);
        goodsorder.setCode(String.valueOf((int)(Math.random()*8998)+1000+1));
        goodsorder.setState("待收货");
        goodsorder.setWords(words);
        goodsorder.setNumber(1);
        goodsorder.setTotal(points);
        goodsorder.setUid(id);
        ServiceAddress serviceAddress2 = serviceAddressService.selectByAddressGetId(address);
        if(serviceAddress2==null){
            ServiceAddress serviceAddress = new ServiceAddress();
            serviceAddress.setUid(id);
            serviceAddress.setDaddress(address);
            serviceAddress.setCommnuityId(communityId);
            serviceAddressService.insert(serviceAddress);
            goodsorder.setLocationId(serviceAddress.getId());
        }else{
            goodsorder.setLocationId(serviceAddress2.getId());
        }
        goodsorderService.insert(goodsorder);
        Goods goods1 = goodsRepository.findGoodsByIdEquals(gid);
        goods1.setCounts(goods1.getCounts()-points/goods1.getNeedPoints());
        goodsDao.insertOrUpdate(goods1);
        return nowPoints;
    }


}
